package com.wcx.create.singleton;

/**
 * 项目: design-pattern-githupdemo
 * <p>
 * 功能描述: 双重检查机制
 *
 * @author: WuChengXing
 * @create: 2021-01-11 23:14
 **/
public class DoubleCheckSingleton {
    /**
     * 懒汉式变种，属于懒汉式中最好的写法，保证了：延迟加载和线程安全
     */
    private static DoubleCheckSingleton instance=null;

    private DoubleCheckSingleton() {};

    /**
     * 通过两次检查，有效的避免了出现两次实例化对象的情况
     * @return
     */
    public static DoubleCheckSingleton getInstance(){
        if (instance == null) {
            synchronized (DoubleCheckSingleton.class) {
                if (instance == null) {
                    instance = new DoubleCheckSingleton();
                }
            }
        }
        return instance;
    }

}
